<?php
// 应用公共文件

use think\exception\HttpResponseException;

 /**
 * 操作成功跳转
 * @param  mixed     $msg 提示信息
 * @param  string    $url 跳转的URL地址
 * @param  mixed     $data 返回的数据
 * @param  integer   $wait 跳转等待时间
 * @param  array     $header 发送的Header信息
 * @return void
 */
function success($msg = '', string $url = null, $data = '', int $wait = 3, array $header = []): Response
{
  if (is_null($url) && isset($_SERVER["HTTP_REFERER"])) {
    $url = $_SERVER["HTTP_REFERER"];
  } elseif ($url) {
    $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : app('route')->buildUrl($url)->__toString();
  }

  $result = [
    'code' => 1,
    'msg'  => $msg,
    'data' => $data,
    'url'  => $url,
    'wait' => $wait,
  ];

  $type = (request()->isJson() || request()->isAjax()) ? 'json' : 'html';
  if ($type == 'html'){
    $response = view(app('config')->get('app.dispatch_success_tmpl'), $result);
  } else if ($type == 'json') {
    $response = json($result);
  }
  throw new HttpResponseException($response);
}

function error($msg = '', string $url = null, $data = '', int $wait = 3, array $header = []): Response
{
  if (is_null($url)) {
    $url = request()->isAjax() ? '' : 'javascript:history.back(-1);';
  } elseif ($url) {
    $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : app('route')->buildUrl($url)->__toString();
  }

  $result = [
    'code' => 0,
    'msg'  => $msg,
    'data' => $data,
    'url'  => $url,
    'wait' => $wait,
  ];

  $type = (request()->isJson() || request()->isAjax()) ? 'json' : 'html';
  if ($type == 'html'){
    $response = view(app('config')->get('app.dispatch_error_tmpl'), $result);
  } else if ($type == 'json') {
    $response = json($result);
  }
  throw new HttpResponseException($response);
}

/**
 * 过滤数组元素前后空格 (支持多维数组)
 * @param $array 要过滤的数组
 * @return array|string
 */
function trim_array_element($array)
{
  if (!is_array($array)) 
    return trim($array);
  return array_map('trim_array_element', $array);
}

/**
 * 获取目录里的文件，不包括下级文件夹
 * @param string $dir  路径
 * @return array
 */
function get_dir($dir){
  $file = @ scandir($dir);
  foreach ($file as $key){
    if ( $key != ".." && $key != "." ){
      $files[] = $key;
    }
  }
  return $files;
}

/**
 * 根据数组中某个字段重新分组
 * @param {dataArr:需要分组的数据；keyStr:分组依据}
 * @return: array
 */
function array_group(array $dataArr, string $keyStr)   :array
{
  $newArr=[];
  foreach ($dataArr as $k => $val) {
    $newArr[$val[$keyStr]][] = $val;
  }
  return $newArr;
}

/**
 * 替换手机号码中间四位数字
 * @param  [type] $str [description]
 * @return [type]      [description]
 */
function hide_phone($str){
  $resStr = substr_replace($str,'****',3,4);
  return $resStr;
}

/**
 * 传递一个父级分类ID返回所有子分类
 * @param $cate
 * @param $pid
 * @return array
 */
function getChildsRule($rules, $pid)
{
  $arr = [];
  foreach ($rules as $v) {
    if ($v['pid'] == $pid) {
      $arr[] = $v;
      $arr = array_merge($arr, getChildsRule($rules, $v['id']));
    }
  }
  return $arr;
}

/**
 * 根据附件表的id返回url地址
 * @param  [type] $id [description]
 * @return [type]     [description]
 */
// function getUrl($id)
// {
// 	if ($id) {
// 		$getUrl = \think\Db::name("attachment")->where(['id' => $id])->find();
// 		if($getUrl['status'] == 1) {
// 			//审核通过
// 			return $getUrl['filepath'];
// 		} elseif($getUrl['status'] == 0) {
// 			//待审核
// 			return '/uploads/xitong/beiyong1.jpg';
// 		} else {
// 			//不通过
// 			return '/uploads/xitong/beiyong2.jpg';
// 		} 
//     }
//     return false;
// }